package com.hxzy.util.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

@Controller
public class UtilController {

    @RequestMapping("getImage.do")
    public void getImage(String path, HttpServletResponse response){
        if(path.endsWith("jpg")) {
            response.setContentType("image/jpeg");
        }else if(path.endsWith("png")) {
            response.setContentType("image/png");
        }else {
            response.setContentType("image/*");
        }

        File file = new File("D:\\erp_system\\image", path);
        InputStream is = null;
        OutputStream os = null;
        try{
            is = new FileInputStream(file);
            byte[] b = new byte[1024];
            int len;
            os = response.getOutputStream();
            while((len = is.read(b)) != -1) {
                os.write(b, 0, len);
            }
            is.close();
            os.close();
        }catch (Exception e){
            System.out.println("图片获取发生异常:  " + e.getMessage());
        }finally {
            try {
                if(is != null) {
                    is.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(os != null) {
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

}
